<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
        >
          <h1>Hello</h1>
          <h2>后台管理系统模板项目</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              placeholder="账号"
              size="large"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              placeholder="密码"
              size="large"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              :loading="loading"
              class="login-btn"
              type="primary"
              size="large"
              @click="doLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import router from '@/router'
import { ElNotification } from 'element-plus'
import { responseType } from '@/api'
import { getGreeting } from '@/utils/datetimeUtils'

let loginFormRef = ref()
const loading = ref(false)
let useStore = useUserStore()
let loginForm = reactive({
  username: '',
  password: '',
})

const validatorUsername = (rules: any, value: any, callback: any) => {
  // rules - 校验规则对象
  // value - form-item 的值
  // callback - 回调函数，不管是否验证通过都使用该回调函数
  //            失败的话注意返回出错信息
  if (/^\d{4,20}$/.test(value)) callback()
  else callback(new Error('请保证账号长度在 4 - 20 之间！'))
}

const rules = {
  username: [
    // 下面是规范内校验方式
    // { required: true, message: '用户名不能为空！', trigger: 'blur' },
    // {
    //   min: 4,
    //   max: 20,
    //   message: '请保证账号长度在4 - 20 之间',
    //   trigger: 'change',
    // },

    // 下面是自定义校验
    { required: true, validator: validatorUsername, trigger: 'change' },
  ],
  password: [{ required: true, message: '密码不能为空！', trigger: 'blur' }],
}

const doLogin = async () => {
  const promise = await loginFormRef.value.validate()

  loading.value = true

  useStore.login(loginForm).then(
    (res: responseType) => {
      if (res.code === 1) {
        router.push('/')

        const greeting: string = getGreeting() || ''
        ElNotification.success(greeting)
      } else {
        ElNotification.error('账号或密码不正确！')
      }

      loading.value = false
    },
    (err) => {
      console.log(
        '登录页面接受的来自 store 登录返回的 Promise 对象响应结果：',
        err,
      )
      loading.value = false
    },
  )
}
</script>
<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 60%;
    top: 30vh;
    background: url('@/assets/images/login_form.png');
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login-btn {
      width: 100%;
    }
  }
}
</style>
